﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Taobao.Public
{
    public class LogHelper
    {
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="txt"></param>
        public static void ErrorLog(string txt)
        {
            string path = HttpRuntime.AppDomainAppPath.ToString() + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

            WriteCotent(txt, path);
        }
        /// <summary>
        /// 访问记录
        /// </summary>
        /// <param name="txt"></param>
        public static void ScanLog(string txt)
        {
            string path = HttpRuntime.AppDomainAppPath.ToString() + @"\ScanLog\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

            WriteCotent(txt, path);
        }
        public static void WriteCotent(string txt, string path)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);

                }
                path += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

                if (!File.Exists(path))
                {
                    using (FileStream fscreate = new FileStream(path, FileMode.Create))
                    {
                        fscreate.Close();
                    }
                }
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");
                        sw.Close();
                    }
                    fs.Close();
                }
            }

            catch (Exception ex)
            {
                WriteCotent("程序发生异常（WriteLog）。详情：" + ex.Message, path);

            } 
        }
    }
}